package com.rimi.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rimi.bean.BookBean;
import com.rimi.bean.PageBean;
import com.rimi.service.impl.BookServiceImpl;
import com.rimi.util.ExportToExcel;

@Controller
public class BookController {
	@Autowired
	private BookServiceImpl bs;

	@RequestMapping("bookInfo")
	public String change1(BookBean bb, Model m, PageBean pb, String isok) {
		// 这里isOK不能直接用button的点击事件触发location.href传参,会造成数据遗失
		if ("clean".equals(isok)) {
			bb = null;
		}
		// 用这个方法得到count,添加进pageBean的属性当中,
		List list = bs.getBookAllInfo(bb);
		pb.setCount(list.size());
		m.addAttribute("pb", pb);
		list = bs.getBookInfo(bb, pb);
		// 在页面上遍历list
		m.addAttribute("info", list);
		// 为了数据回显
		m.addAttribute("book", bb);
		return "Book/index";
	}

	@RequestMapping("getAddBook")
	public String bookInfo(BookBean bb) {
		return "Book/add";
	}

	@RequestMapping("addBook")
	public String addBook(BookBean bb) {
		bs.addBook(bb);
		return "redirect:bookInfo";
	}

	@RequestMapping("checkBookCode")
	public void checkBookCode(String code, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		BookBean bb = bs.getBookByCode(code);
		try {
			PrintWriter out = response.getWriter();
			if (bb != null) {
				out.println(1);
			} else {
				out.println(0);
			}
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("delBook")
	public String delBook(String[] codes) {
		// 前段所带过来的参数名叫codes的会被自动封装成String [] codes
		// 凡是从页面跳到servlet中的参数,名字相同的都会被封装成一个同名的数组,
		// 凡是从页面跳到servlet中的参数,有和bean中的属性相同的都会被封装成对象传过来
		bs.delBook(codes);
		return "redirect:bookInfo";

	}

	@RequestMapping("ExportToExcel")
	public void ExportToExcel(String[] codes, HttpServletResponse response) {
		List li = bs.toExcel(codes);

		String title = "书籍信息表";
		String[] rowsName = { "book_id", "book_code", "book_name", "book_number", "book_price", "book_author",
				"book_adress", "book_url", "book_remark", "book_price2" };
		List BookInfo = new ArrayList();
		Object[] obj = null;

		for (int i = 0; i < li.size(); i++) {
			obj = new Object[rowsName.length];
			BookBean b = (BookBean) li.get(i);
			obj[0] = b.getBookId();
			obj[1] = b.getBookCode();
			obj[2] = b.getBookName();
			obj[3] = b.getBookNumber();
			obj[4] = b.getBookPrice();
			obj[5] = b.getBookAuthor();
			obj[6] = b.getBookAddress();
			obj[7] = b.getBookUrl();
			obj[8] = b.getBookRemark();
			obj[9] = b.getBookPrice2();

			BookInfo.add(obj);

		}
		ExportToExcel ete = new ExportToExcel(title, rowsName, BookInfo, response);
		ete.exportData();
		//return "redirect:bookInfo";
	}
}
